﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class UcQuanLyChiTieu : UserControl
    {
        public UcQuanLyChiTieu()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        //bool themchi = true;
        //bool themchitiet = true;        
        private void Loaddata()
        {
            gridControlChi.DataSource = cn.getDataTable("tblChiTieu_DS");
            chitietchibinding(int.Parse(gridViewChi.GetFocusedRowCellDisplayText("MaChi").ToString()));
        }
        private void UcQuanLyChiTieu_Load(object sender, EventArgs e)
        {
            Loaddata();
               

        }       
        private void chitietchibinding(int machi)
        {
            SqlCommand cmd = new SqlCommand("tblChiTietChiTieu_DS", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("machi", SqlDbType.Int)).Value = machi;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridControlChiTiet.DataSource = dt;
        }
        private void gridViewChi_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            lbMaChi.Text = gridViewChi.GetFocusedRowCellDisplayText("MaChi");
            lbNgayChi.Text = gridViewChi.GetFocusedRowCellDisplayText("NgayChi").ToString();
            lbNguoiChi.Text = gridViewChi.GetFocusedRowCellDisplayText("HoTen");
            
            try
            {
                chitietchibinding(int.Parse(gridViewChi.GetFocusedRowCellValue("MaChi").ToString()));
            }
            catch
            {
            }
        }

        private void gridViewChi_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gridViewChi.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.DeepSkyBlue;
            }
        }

        private void gridViewChiTiet_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                lbTenChi.Text = gridViewChiTiet.GetFocusedRowCellDisplayText("TenDungCu");
                lbSoLuong.Text = String.Format("{0:N0}",double.Parse(gridViewChiTiet.GetFocusedRowCellDisplayText("SoLuong")));
                lbDonGia.Text = String.Format("{0:N0}",double.Parse(gridViewChiTiet.GetFocusedRowCellDisplayText("DonGia")));
                lbThanhTien.Text = String.Format("{0:N0}",(double.Parse(lbSoLuong.Text) * double.Parse(lbDonGia.Text)));
            }
            catch
            {
            }
        }

        private void gridViewChiTiet_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle ==gridViewChiTiet.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.LightSeaGreen;
            }
        }

        private void txtTim_Enter(object sender, EventArgs e)
        {
            if (txtTim.Text == "Nhập từ khóa tìm kiếm")
            {
                txtTim.Text = "";
            }
        }

        private void txtTim_Leave(object sender, EventArgs e)
        {
            if (txtTim.Text == "")
            {
                txtTim.Text = "Nhập từ khóa tìm kiếm";
            }
        }

        private void dtTim_ValueChanged(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("tblChiTieu_TimNgay", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            dtTim.CustomFormat="dd/MM/yyyy";
            dtTim.Format=DateTimePickerFormat.Custom;
            cmd.Parameters.Add(new SqlParameter("ngay", SqlDbType.Int)).Value = dtTim.Value.Day;
            cmd.Parameters.Add(new SqlParameter("thang", SqlDbType.Int)).Value = dtTim.Value.Month;
            cmd.Parameters.Add(new SqlParameter("nam", SqlDbType.Int)).Value = dtTim.Value.Year;            
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridControlChi.DataSource = dt;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            Loaddata();
            txtTim.Text = "Nhập từ khóa tìm kiếm";
        }

        private void btnThemChi_Click(object sender, EventArgs e)
        {
            frmChiTietChiTieu _frm = new frmChiTietChiTieu();
            _frm.ShowDialog();
            Loaddata();
        }

        private void btnLuuChi_Click(object sender, EventArgs e)
        {
            
        }

        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            if (txtTim.Text != "Nhập từ khóa tìm kiếm")
            {
                SqlCommand cmd = new SqlCommand("tblChiTieu_TimKiemTen",cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("timten",SqlDbType.NVarChar)).Value = txtTim.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gridControlChi. DataSource = dt;
            }
        }

        private void btnSuaChi_Click(object sender, EventArgs e)
        {            
            bientinhChiTieu.Machi = gridViewChi.GetFocusedRowCellDisplayText("MaChi");
            bientinhChiTieu.Hoten = gridViewChi.GetFocusedRowCellDisplayText("HoTen");
            bientinhChiTieu.Manv = gridViewChi.GetFocusedRowCellDisplayText("MaNV");
            bientinhChiTieu.Ngaychi = Convert.ToDateTime(gridViewChi.GetFocusedRowCellDisplayText("NgayChi"));
            bientinhChiTieu.Tongtien = Convert.ToInt32(gridViewChi.GetFocusedRowCellDisplayText("TongTien"));
            bientinhChiTieu.Bangchu = cn.DocSo(Convert.ToDouble(gridViewChi.GetFocusedRowCellDisplayText("TongTien")));
            frmSuaChiTieu _frmSuaChiTieu = new frmSuaChiTieu();
            _frmSuaChiTieu.ShowDialog();
            Loaddata();

        }

        private void btnInPhieuChi_Click(object sender, EventArgs e)
        {
            bientinhChiTieu.Machi = gridViewChi.GetFocusedRowCellDisplayText("MaChi");
            bientinhChiTieu.Hoten = gridViewChi.GetFocusedRowCellDisplayText("HoTen");
            bientinhChiTieu.Manv = Convert.ToString(gridViewChi.GetRowCellValue(gridViewChi.FocusedRowHandle, "MaNV"));
            bientinhChiTieu.Tongtien = Convert.ToInt32(gridViewChi.GetFocusedRowCellDisplayText("TongTien"));
            bientinhChiTieu.Ngaychi = Convert.ToDateTime(gridViewChi.GetFocusedRowCellDisplayText("NgayChi"));
            bientinhChiTieu.Bangchu=cn.DocSo(Convert.ToDouble(gridViewChi.GetFocusedRowCellDisplayText("TongTien")));
            frmInPhieuChi _frminphieuchi = new frmInPhieuChi();
            _frminphieuchi.ShowDialog();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa phiếu chi này!\nNếu bạn chọn YES thì các cả chi tiết chi tiêu liên quan sẽ bị xóa hết!!!", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                SqlCommand cmd = new SqlCommand("tblChiTieu_Xoa", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("machi", SqlDbType.Int)).Value = gridViewChi.GetFocusedRowCellDisplayText("MaChi");
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Phiếu chi đã được xóa thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    Loaddata();
                }
            }
        }
    }
}
